package com.ideabove.heart.custom;

import com.ideabove.heart.R;
import com.ideabove.heart.manager.MoeManager;
import com.ideabove.heart.manager.ScheduleManager;
import com.ideabove.heart.util.LogUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class FreeCloset extends ImageView{
	
	public String dress;
	public String hair;
	public String desc;
	public String title;
	public int opdate;
	public String display;
	
	
	public Bitmap openBg;
	public Bitmap closeBg;
	public Bitmap focusBg;
	
	// ribon
	public String ribonName;
	public String ribonBig;
	public Bitmap ribon;
	public Rect sRect;
	
	
	
	public int alarmCnt;
	
	public boolean isFocus;

	public FreeCloset(Context context, AttributeSet attrs) {
		super(context, attrs);
		

		dress = context.obtainStyledAttributes( attrs, R.styleable.FreeCloset )
		.getString( R.styleable.FreeCloset_dress);
		
		hair = context.obtainStyledAttributes( attrs, R.styleable.FreeCloset )
		.getString( R.styleable.FreeCloset_hair);
		
		desc = context.obtainStyledAttributes( attrs, R.styleable.FreeCloset )
		.getString( R.styleable.FreeCloset_desc);
		
		title = context.obtainStyledAttributes( attrs, R.styleable.FreeCloset )
		.getString( R.styleable.FreeCloset_title);
		
		opdate = Integer.parseInt(context.obtainStyledAttributes( attrs, R.styleable.FreeCloset )
		.getString( R.styleable.FreeCloset_opdate));
		
		display = context.obtainStyledAttributes( attrs, R.styleable.FreeCloset )
		.getString( R.styleable.FreeCloset_display);
		
		ribonName = context.obtainStyledAttributes( attrs, R.styleable.FreeCloset )
		.getString( R.styleable.FreeCloset_ribon) ;
		
		ribonBig = context.obtainStyledAttributes( attrs, R.styleable.FreeCloset )
		.getString( R.styleable.FreeCloset_ribonbig) ;
		
		openBg = BitmapFactory.decodeResource(getResources(), R.drawable.shop_box_white);
		closeBg = BitmapFactory.decodeResource(getResources(), R.drawable.shop_box_gray);
		focusBg = BitmapFactory.decodeResource(getResources(), R.drawable.shop_box_select);
		
		// ribon
		ribon = MoeManager.loadDataFromAsset(ribonName + ".png");
		if(ribon != null){
			sRect = new Rect(0,0,ribon.getWidth(),ribon.getHeight());			
		}
		
		alarmCnt = new ScheduleManager((Activity)context).getLastSchedule().getId();
	}
	
	protected void drawSelf(Canvas canvas){
		// 모닝콜 오픈 날짜에 따라 배경색을 몰로 할지를 결정한다.
		Rect dst = new Rect(0,0,getWidth(),getHeight());		
		if(alarmCnt > opdate){
			if(openBg != null){
				Rect src = new Rect(0,0,openBg.getWidth(),openBg.getHeight());			
				canvas.drawBitmap(openBg, src, dst, null);
			}
		}else{ 
			if(openBg != null){
				Rect src = new Rect(0,0,closeBg.getWidth(),closeBg.getHeight());					
				canvas.drawBitmap(closeBg, src, dst, null);
			}
		}	
		
		if(isFocus){
			Rect src = new Rect(0,0,focusBg.getWidth(),focusBg.getHeight());
			canvas.drawBitmap(focusBg, src, dst, null);
		}
	}
	
	

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		drawSelf(canvas);
		super.onDraw(canvas);		
		
		
		if(ribon != null){
			canvas.drawBitmap(ribon,sRect,new Rect(0,0,getWidth(),getHeight()),null);
		}
	}




	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		
		return super.onTouchEvent(event);
	}
	
	

}
